package com.kedacom.freedomLabel.utils;


import android.text.TextUtils;

public class SerialPortSplice {


    public static byte[] ByteJoin(String numStr, String unitStr) {
        if (numStr == null || TextUtils.isEmpty(numStr)) {
            return null;
        }

        //            System.out.println("numStr"+numStr.trim());

        if (numStr.contains("OL")) {

            String hexStrOl = string2HexString("+oL");
            byte[] hexToByteOl = hexToByteArray(hexStrOl);

            byte[] hexToByteArrayoL = new byte[hexToByteOl.length + 2];

            System.arraycopy(hexToByteOl, 0, hexToByteArrayoL, 0, hexToByteOl.length);


            hexToByteArrayoL[hexToByteOl.length] = 0x0D;
            hexToByteArrayoL[hexToByteOl.length + 1] = 0x0A;

            return hexToByteArrayoL;

        } else {

            if (Double.parseDouble(numStr) >= 0) {
                numStr = "+" + numStr.trim();
            }else {
                numStr = numStr.trim();
            }
            String hexUnitStr = string2HexString(unitStr);
            byte[] hexToByteUnitStr = hexToByteArray(hexUnitStr);

            String hexString = string2HexString(numStr);
            byte[] hexToByteArray = hexToByteArray(hexString);

            byte[] hexToByteArray01 = new byte[hexToByteArray.length + hexToByteUnitStr.length + 2];

            System.arraycopy(hexToByteArray, 0, hexToByteArray01, 0, hexToByteArray.length);

            System.arraycopy(hexToByteUnitStr, 0, hexToByteArray01, hexToByteArray.length, hexToByteUnitStr.length);

            hexToByteArray01[hexToByteArray.length + hexToByteUnitStr.length] = 0x0D;
            hexToByteArray01[hexToByteArray.length + hexToByteUnitStr.length + 1] = 0x0A;

            return hexToByteArray01;

        }






    }

    /**
     * hex字符串转byte数组
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte数组结果
     */
    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            //奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            //偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }

    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    /**
     * 字符串转16进制字符串
     *
     * @param strPart
     * @return
     */
    public static String string2HexString(String strPart) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < strPart.length(); i++) {
            int ch = (int) strPart.charAt(i);
            String strHex = Integer.toHexString(ch);
            hexString.append(strHex);
        }
        return hexString.toString();
    }

    public static byte[] hexString2Bytes(String hexString) {
        int len = hexString.length();
        if (len % 2 != 0) {
            hexString = "0" + hexString;
            len = len + 1;
        }
        char[] hexBytes = hexString.toUpperCase().toCharArray();
        byte[] ret = new byte[len >> 1];
        for (int i = 0; i < len; i += 2) {
            ret[i >> 1] = (byte) (hex2Int(hexBytes[i]) << 4 | hex2Int(hexBytes[i + 1]));
        }
        return ret;
    }

    private static int hex2Int(final char hexChar) {
        if (hexChar >= '0' && hexChar <= '9') {
            return hexChar - '0';
        } else if (hexChar >= 'A' && hexChar <= 'F') {
            return hexChar - 'A' + 10;
        } else {
            throw new IllegalArgumentException();
        }
    }

}
